package com.hnit.care.handler;

import com.hnit.care.config.WebSocketHandler;
import com.hnit.care.entity.Coredata;
import com.hnit.care.entity.Drug;
import com.hnit.care.mapper.CoredataMapper;
import com.hnit.care.mapper.DrugMapper;
import com.hnit.care.netty.NettyServerHandler;
import com.hnit.care.service.IDrugService;
import com.hnit.care.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Date;

/**
 * 由于时间原因，该项目采用技能赛那一套方案，三端交互采用本类来处理数据
 */
@Component
public class HandlerData {
    @Resource
    private CoredataMapper coredataMapper;

    @Resource
    private DrugMapper drugMapper;

    @Resource
    private IDrugService drugService;

    @Autowired
    private WebSocketHandler webSocketHandler;

    @Autowired
    private NettyServerHandler nettyServerHandler;

    @Resource
    private SmsService smsService;
    Coredata coredata = new Coredata();

    Drug drug = new Drug();



    /**
     *  TODO 以下五个功能点需要完成     
     * @param msg 传过来的数据帧数据
     * @throws Exception
     */
    public void handlerMsg(String msg) throws Exception {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        /**
         * 功能点1：将硬件传过来的数据存到数据库并转发给所有的客户端
         *
         *    Hax+0050ay-1000az+0005gx-2300gy+1234gz-4444T
         *
         *    ax,ay,az : X/Y/Z轴的加速度
         *    gx,gy,gz : X/Y/Z轴的角速度
         *
         *    Hhr120sp080T
         *    hr心率; sp血氧浓度
         */

        /**
         * 功能点2：判断血氧浓度，若出现异常，就发一条帧报警
         */
        //Hhr120sp080T
       if (msg.matches("Hhr.{8}T")){
           String heartRate = msg.substring(3,6);
           String bloodOxygenation = msg.substring(8,11);

           if (Integer.valueOf(bloodOxygenation) <= 90){
               nettyServerHandler.doAllReplay("HbloodoxylowT");
           }

           coredata.setHeartRate(heartRate)
                   .setBloodOxygenation(bloodOxygenation);

           coredataMapper.insert(coredata);
       }
       /**
        * 功能点3：若是老人摔倒，发送短信
        */
       else if (msg.matches("HgyroerrorT")) {
           // 验证码
           String code = "";
           // 过期时间
           String expireTime = "";
           // param中code表示验证码,expire_at代表过期时间
           String[] params = {code, expireTime};
           String phone = "18639960035";
           smsService.sendSms(phone,params);
       }
       /**
        * 功能点4：发送时间给药盒，就是说 吃药的时间+吃什么药+吃药的数量，这条数据帧由app那端发送，我们解析存到数据库
        */
       //HH00M00S00h00m00s00pill***count123T
       else if (msg.matches("HH.{32}T")) {
           System.out.println(msg);
           String H = msg.substring(2,4);
           String M = msg.substring(5,7);
           String S = msg.substring(8,10);
           String drugTime = H + ":" + M + ":" + S;
           System.out.println(drugTime);

           String h = msg.substring(11,13);
           String m = msg.substring(14,16);
           String s = msg.substring(17,19);
           String endtime = h + ":" + m + ":" +s;

           String drugName = msg.substring(23,26);
           Integer drugNum = Integer.valueOf(msg.substring(31,34));

           //Date start = format.parse(drugTime);
           LocalTime time = LocalTime.parse(drugTime);

           System.out.println(time+"111");
           Date end = format.parse(endtime);

           drug.setDrugName(drugName)
                   .setDrugNum(drugNum)
                   .setDrugTime(time);

           drugMapper.insert(drug);

       }
        /**
         * 功能点5：药盒打开一次，就要相应更新数据库，保证数据的一致性
         */
        //H openpill***count00 T
       else if (msg.matches("H.{18}T")) {
           String drugName = msg.substring(9,12);
           Integer count = Integer.valueOf(msg.substring(17,19));

           Drug pill = drugService.selectdrug(drugName);
           int drugnum = pill.getDrugNum() - count;

           if (drugnum <= 2){
               nettyServerHandler.doAllReplay("HpilllessT");
           }

           drugService.updatacount(drugName,drugnum);
       }
    }
}
